package com.qcby.llmtchstd.service.Impl;


import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.dao.TeacherDao;
import com.qcby.llmtchstd.entity.Student;
import com.qcby.llmtchstd.entity.Teacher;
import com.qcby.llmtchstd.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Override
    public ResponseResult count() {
        try {
            Integer num = teacherDao.count();
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "查询教师数量失败，结果为空"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询教师数量失败"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult page(Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        try {
            List<Teacher> list=teacherDao.page(offset,pageSize);
            return ResponseResult.success(list);
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询教师数量失败");
        }
    }

    @Override
    public ResponseResult add(Teacher teacher) {
        try{
            int num=teacherDao.add(teacher);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","添加教师失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询教师数量失败");
        }
    }

    @Override
    public ResponseResult search(Teacher teacher) {
        try {
            List<Teacher> list=teacherDao.search(teacher);
            if(list.size()!=0){
                return ResponseResult.success(list);
            }
            return ResponseResult.fail("4003","教师不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询教师数量失败");
        }
    }

    @Override
    public ResponseResult update(Teacher teacher) {
        try{
            int num=teacherDao.update(teacher);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","修改教师失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：修改教师数量失败");
        }
    }


    @Override
    public ResponseResult delete(Integer id) {
        try{
            int num=teacherDao.delete(id);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","删除教师失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：删除教师数量失败");
        }
    }

    @Override
    public ResponseResult edit(Integer id) {
        try {
            Teacher temp=teacherDao.edit(id);
            if(temp.getName()!=null){
                return ResponseResult.success(temp);
            }
            return ResponseResult.fail("4003","教师不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询教师数量失败");
        }
    }

    @Override
    public ResponseResult serCount(Teacher teacher) {
        try {
            Integer num = teacherDao.serCount(teacher);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败！"); // 统一处理数据库异常
        }
    }
}
